/* ================ 深拷贝 ================ */
//返回传递给他的任意对象的类
const isClass = o => {
	if (o === null) return "Null";
	if (o === undefined) return "Undefined";
	return Object.prototype.toString.call(o).slice(8, -1);
};

function mySystemInfo() {
  var info = {}
  try {
    info = uni.getSystemInfoSync()
    console.log('info是什么',info)
  } catch (e) {
    console.error(e)
  }
  return info
}

//深度克隆
const deepClone = obj => {
	if (!obj) {
		return null;
	}
	let result,
		oClass = isClass(obj);
	//确定result的类型
	if (oClass === "Object") {
		result = {};
	} else if (oClass === "Array") {
		result = [];
	} else {
		return obj;
	}

	for (let key in obj) {
		if (obj.hasOwnProperty(key)) {
			let copy = obj[key];
			if (isClass(copy) == "Object") {
				result[key] = deepClone(copy); //递归调用
			} else if (isClass(copy) == "Array") {
				result[key] = deepClone(copy);
			} else {
				result[key] = obj[key];
			}
		}
	}
	return result;
};

// 身份证验证
function ValidateIdCard(idcard) {
	const reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
	return reg.test(idcard);
}
//  开户行账号格式校验

function checkOpeningBankAccount(account) {
	const reg = /^([1-9]{1})(\d{11}|\d{15}|\d{16}|\d{17}|\d{18})$/;
	return reg.test(account);
}
// 邮箱正则校验
function email(email) {
	const reg = /^[A-Za-z0-9-._]+@[A-Za-z0-9-]+(.[A-Za-z0-9]+)*(.[A-Za-z]{2,6})$/;
	return reg.test(email);
}
// 手机正则校验
function telPhone(phone) {
	const reg = /^1[3|4|5|7|8][0-9]{9}$/;
	return reg.test(phone);
}

function time_to_sec(time) {
	var s = "";

	var hour = time.split(":")[0];
	var min = time.split(":")[1];
	var sec = time.split(":")[2];

	s = Number(hour * 3600) + Number(min * 60) + Number(sec);

	return s;
}
// 获取当天的前七天日期函数
function getBeforeDate(n) {
	console.log("开始调用日期函数了", n);
	var n = n;
	var d = new Date();
	var year = d.getFullYear();
	var mon = d.getMonth() + 1;
	var day = d.getDate();
	if (day <= n) {
		if (mon > 1) {
			mon = mon - 1;
		} else {
			year = year - 1;
			mon = 12;
		}
	}
	d.setDate(d.getDate() - n);
	year = d.getFullYear();
	mon = d.getMonth() + 1;
	day = d.getDate();
	var s =
		year +
		"-" +
		(mon < 10 ? "0" + mon : mon) +
		"-" +
		(day < 10 ? "0" + day : day);
	return s;
}
// 时间戳转换成日期时间
function getYMDHMS(timestamp) {
	let time = new Date(timestamp);
	let year = time.getFullYear();
	let month = time.getMonth() + 1;
	let date = time.getDate();
	let hours = time.getHours();
	let minute = time.getMinutes();
	let second = time.getSeconds();
	if (month < 10) {
		month = "0" + month;
	}
	if (date < 10) {
		date = "0" + date;
	}
	if (hours < 10) {
		hours = "0" + hours;
	}
	if (minute < 10) {
		minute = "0" + minute;
	}
	if (second < 10) {
		second = "0" + second;
	}
	return (
		year + "-" + month + "-" + date + " " + hours + ":" + minute + ":" + second
	);
}

function timestampToTime(timestamp) {
	var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + "-";
	var M =
		(date.getMonth() + 1 < 10 ?
			"0" + (date.getMonth() + 1) :
			date.getMonth() + 1) + "-";
	var D = date.getDate() + " ";
	var h = date.getHours() + ":";
	var m = date.getMinutes() + ":";
	var s = date.getSeconds();
	return Y + M + D + h + m + s;
}

function year() {
	var myDate = new Date();
	var year = myDate.getYear(); //获取当前年份(2位)
	return year;
}

// 格式化千分位  保留两位小数
function toThousands(value) {
	if (!value) return '0.00';

	var intPart = Number(value) | 0; //获取整数部分

	let splitNums = parseFloat(value).toFixed(2).split('.');

	var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); //将整数部分每三位加一个逗号

	var floatPart = ".00"; //预定义小数部分

	if (typeof value !== 'string') {

		value = value.toString();

	}

	if (value == 0) {

		value = 0.00

	}

	var value2Array = value.split(".");

	//=2表示数据有小数位

	if (value2Array.length == 2) {

		floatPart = value2Array[1].toString(); //拿到小数部分

		if (floatPart.length > 2) { //若小数位数在两位以上，则四舍五入保留两位小数

			return intPartFormat + "." + splitNums[1];

		} else if (floatPart.length == 1) { //若小数位数只有一位，补0

			return intPartFormat + "." + floatPart + '0';

		} else {

			return intPartFormat + "." + floatPart;

		}

	} else { //数据为整数

		return intPartFormat + floatPart;

	}
}
// 格式化成年月日
// 使用方式
// var year = getDateTime('year');  
// console.log(year);  // 2021
// var month = getDateTime('month');  
// console.log(month); // 2021-12
// var date = getDateTime('');
// console.log(date);  // 2021-12-03 09:00:00
// 日期格式化年月日函数
function formatDate(type, format) {
	var date = new Date();
	var hengGang = "-";
	var maoHao = ":";
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var curDate = date.getDate();
	var curHours = date.getHours();
	var curMinutes = date.getMinutes();
	var curSeconds = date.getSeconds();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (curDate >= 0 && curDate <= 9) {
		curDate = "0" + curDate;
	}
	if (curHours >= 0 && curHours <= 9) {
		curHours = "0" + curHours;
	}
	if (curMinutes >= 0 && curMinutes <= 9) {
		curMinutes = "0" + curMinutes;
	}
	if (curSeconds >= 0 && curSeconds <= 9) {
		curSeconds = "0" + curSeconds;
	}
	var currentdate = "";
	if (type == 'format') {
		// 传递需要的日期 格式化成年月日 只要年月日  format是传递的日期参数
		currentdate = format.slice(0, 16)
		return currentdate;
	} else if (type == "year") {
		// 获取年份
		currentdate = year;
		return currentdate;
	} else if (type == "month") {
		// 获取年份月份
		currentdate = year + hengGang + month
		return currentdate
	} else if (type == "day") {
		// 获取年月日
		currentdate = year + hengGang + month + hengGang + curDate
		return currentdate
	} else if (type == "minutes") {
		// 获取年月日 时分
		currentdate =
			year + hengGang + month + hengGang + curDate + ' ' + curHours + maoHao + curMinutes
	} else {
		// 获取年月日 + 当前时间
		currentdate =
			year + hengGang + month + hengGang + curDate + ' ' + curHours + maoHao + curMinutes + maoHao + curSeconds
		return currentdate
	}
}

function month() {
	var myDate = new Date();
	var month = myDate.getMonth();
	return month;
}

function setData(obj, callback) {
	let that = this;
	const handleData = (tepData, tepKey, afterKey) => {
		tepKey = tepKey.split(".");
		tepKey.forEach(item => {
			if (tepData[item] === null || tepData[item] === undefined) {
				let reg = /^[0-9]+$/;
				tepData[item] = reg.test(afterKey) ? [] : {};
				tepData = tepData[item];
			} else {
				tepData = tepData[item];
			}
		});
		return tepData;
	};
	const isFn = function(value) {
		return typeof value == "function" || false;
	};
	Object.keys(obj).forEach(function(key) {
		let val = obj[key];
		key = key.replace(/\]/g, "").replace(/\[/g, ".");
		let front, after;
		let index_after = key.lastIndexOf(".");
		if (index_after != -1) {
			after = key.slice(index_after + 1);
			front = handleData(that, key.slice(0, index_after), after);
		} else {
			after = key;
			front = that;
		}
		if (front.$data && front.$data[after] === undefined) {
			Object.defineProperty(front, after, {
				get() {
					return front.$data[after];
				},
				set(newValue) {
					front.$data[after] = newValue;
					that.$forceUpdate();
				},
				enumerable: true,
				configurable: true
			});
			front[after] = val;
		} else {
			that.$set(front, after, val);
		}
	});
	// this.$forceUpdate();
	isFn(callback) && this.$nextTick(callback);
}
// 金额千分位格式化
function momenyFormat(num) {
	num = Number(num)
	if (isNaN(num)) return '0.00'
	return num.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,')
}

function getMonthRange(monthDay) {
	var now = new Date();
	var year = now.getFullYear();
	var month = now.getMonth() + 1; //0-11表示1-12月
	var day = now.getDate();
	var dateArr = [0, 0];
	dateArr[1] = year + '-' + month + '-' + day;
	var nowMonthDay = new Date(year, month, 0).getDate(); //当前月的总天数
	if (month - monthDay <= 0) { //年数往前推一年
		var lastMonthDay = new Date((year - 1), (12 - (monthDay - parseInt(month))), 0).getDate(); //monthDay个月前所在月的总天数
		if (lastMonthDay < day) { //3个月前所在月的总天数小于现在的天日期
			dateArr[0] = (year - 1) + '-' + (12 - (monthDay - month)) + '-' + lastMonthDay;
		} else {
			dateArr[0] = (year - 1) + '-' + (12 - (monthDay - month)) + '-' + day;
		}
	} else {
		var lastMonthDay = new Date(year, (parseInt(month) - monthDay), 0).getDate(); //monthDay个月前所在月的总天数
		if (lastMonthDay < day) { //3个月前所在月的总天数小于现在的天日期
			if (day < nowMonthDay) { //当前天日期小于当前月总天数,2月份比较特殊的月份
				dateArr[0] = year + '-' + (month - monthDay) + '-' + (lastMonthDay - (nowMonthDay - day));
			} else {
				dateArr[0] = year + '-' + (month - monthDay) + '-' + lastMonthDay;
			}
		} else {
			dateArr[0] = year + '-' + (month - monthDay) + '-' + day;
		}
	}
	return dateArr;
}
module.exports = {
	timestampToTime: timestampToTime,
	ValidateIdCard: ValidateIdCard,
	checkOpeningBankAccount: checkOpeningBankAccount,
	getYMDHMS,
	setData,
	formatDate,
	email,
	telPhone,
	getBeforeDate,
	deepClone,
	isClass,
	momenyFormat,
	getMonthRange,
	// 格式化千分位
	toThousands,
	mySystemInfo
};